<template>
  <div class="submitATool mt-6">
    <div class="rounded-lg px-4 pt-6 pb-7 box-shadow">
      <h3 class="text-2xl text-gray-1000 font-semibold text-center">
        {{$t('submit_enterYour')}}
      </h3>
      <el-form :model="form"
        label-position="top"
        status-icon
        :rules="rules"
        ref="form"
        label-width="100px"
        class="max-w-xl mx-auto mt-6">
        <el-form-item :label="$t('submit_yourGPT')"
          required="required"
          prop="gpt_link">
          <el-input v-model="form.gpt_link"
            :readonly="loading"
            autocomplete="off"
            :placeholder="$t('submit_pleaseEnterYourGptLink')"></el-input>
        </el-form-item>
        <p class="text-md text-gray-1000">{{$t('submit_weWillAutomatically')}}</p>
        <div class="flex justify-between items-center bg-gray-2600 px-4 sm:px-14 py-6 mt-10 submit-ctrl gap-4 flex-wrap rounded-sm">
          <div class="flex-shrink-0">
            <div class="text-3xl text-gray-1000 font-semibold"> {{$t('submit_totalNum', {num: 29})}}</div>
            <p class="text-sm text-gray-1500 mt-1"> {{$t('submit_noQueueGpt')}}</p>
          </div>
          <el-button type="primary"
            class="flex-shrink-0 w-full sm:w-auto"
            :loading="loading"
            @click="submitForm()">
            {{$t('submit_payNum', {num: 29})}}
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'SubmitGpt',
  props: {},
  data() {
    return {
      form: {
        gpt_link: null,
      },
      rules: {
        gpt_link: [
          {
            required: true,
            message: this.$i18n.t('submit_pleaseEnterYourGptLink'),
            trigger: ['change', 'blur'],
          },
        ],
      },
      loading: false,
    }
  },
  computed: {
    ...mapState({
      locale: (state) => state.locale,
      toolify_isLogin: (state) => state.toolify_isLogin,
    }),
  },
  mounted() {
    if (process.browser) {
      let submitForm = localStorage.getItem('submitGptForm')
      if (submitForm) {
        this.form = JSON.parse(submitForm)
      }
      localStorage.removeItem('submitGptForm')
    }
  },
  methods: {
    submitForm() {
      if (!this.toolify_isLogin) {
        localStorage.setItem('submitGptForm', JSON.stringify(this.form))
        this.$router.replace(`/login?from=${this.$route.name}`)
        return false
      }

      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            let res
            let req = {}
            let langPath = `${this.locale !== 'en' ? `/${this.locale}` : ''}`
            req.success_url = `${window.location.origin}${langPath}/payment?type=submitGpt`
            req.cancel_url = `${window.location.origin}${langPath}/submit-gpt`

            req.gpt_link = this.form.gpt_link
            res = await this.$axiosApi.submitGpt(req)

            this.loading = false
            if (
              res &&
              res.code === 200 &&
              res.data &&
              res.data.stripe &&
              res.data.stripe.url
            ) {
              window.location.href = res.data.stripe.url
            }
          } catch (error) {
            this.loading = false
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.submitATool {
  .box-shadow {
    box-shadow: 0px 0px 14px 0px rgba(0, 0, 0, 0.06);
  }
  .el-button-group {
    .el-button {
      font-size: 16px;
      color: #191919;
      background: none;
      &:hover {
        border-color: #7150e6;
        background: none;
        color: #7150e6;
      }
      &.actived {
        border-color: #7150e6;
        background: none;
        color: #7150e6;
        z-index: 1;
      }
    }
  }
  .el-form {
    .el-form-item {
      margin-bottom: 24px;
      .el-form-item__label {
        font-size: 16px;
        font-weight: bold;
        color: #191919;
        line-height: 21px;
        padding-bottom: 14px;
      }
    }
  }
  .submit-ctrl {
    .el-button {
      font-size: 16px;
      max-width: 100%;
      padding-left: 30px;
      padding-right: 30px;
    }
  }
}
</style>
